<template>
  <div>
    <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft" />
    <div class="bgc">
      <div class="container">
        <div class="registersty">账号注册</div>
        <van-form @submit="onSubmit">
          <van-cell-group inset>
            <van-field
              v-model="username"
              name="username"
              label="用户名"
              placeholder="用户名"
              :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
              v-model="password"
              type="password"
              name="password"
              label="密码"
              placeholder="密码"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
          </van-cell-group>
          <div style="margin: 16px">
            <van-button round block type="primary" native-type="submit">
              点击注册
            </van-button>
            <br />
            <!-- <van-button round block @click="register"> 点击注册 </van-button> -->
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>


<script setup>
import { onMounted, ref } from "vue";
import { getregister } from "../api/getlogin";
import { showSuccessToast, showFailToast } from "vant";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { setstorage } from "../utils/local";
const username = ref("");
const password = ref("");
const router = useRouter();
const store = useStore();
const onClickLeft = () => history.back();
const onSubmit = (values) => {
  getregister(values).then((res) => {
    if (res.data.status == 200) {
      showSuccessToast("注册成功！即将前往登录界面");
      setstorage("TOKEN", res.data.time);
      store.commit("setuser", res.data.time);
      router.push({
        path: "/home",
      });
    }
    if (res.data.status == 404) {
      showFailToast("用户已存在！");
    }
  });
};
const register = () => {
  console.log("注册");
};
onMounted(async () => {
  // const { data: res } = await getlogin();
  // console.log(res);
});
</script>
 


<style scoped>
.bgc {
  width: 100%;
  height: 100%;
  /* display: flex;
  justify-content: center;
  align-items: center; */
  margin-top: 200px;
}
.container {
  /* position: relative;
  top: 50%; */
}
.registersty {
  font-size: 2rem;
}
</style>